package DFA;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;


public class demoTest1FAC1 {
    public static void main(String[] args) throws IOException {
        TxtReadIn readIn = new TxtReadIn(21);//要合并的自动机的个数
        File file = new File("F:\\homeworkFormJun\\input\\FWGC.txt");//要合并自动机的位置
        HashMap<Integer, int[][]> integerHashMap  = readIn.txt2String(file) ;//读取该文件流
        //传入forkJoin进行多线程处理
        ForkJoinTask<returnPOJO> task = new dfaSumTask(0,integerHashMap.size()-1,integerHashMap, readIn);
        //返回pojo对象
        returnPOJO pojo = ForkJoinPool.commonPool().invoke(task);
        //提取最后得到的数组（自动机）
        int[][] result = pojo.getTemp();
        ArrayList<Integer> finalMarked = pojo.getList();
        zongHe zongHe = new zongHe();
        Integer[] way = zongHe.getWay(result);
        /*
            后面的代码处理结果
         */
        //选择输出位置
        fileWrite.writeTXTInial("F:\\homeworkFormJun\\output","LXX");
        fileWrite.write("Transitions:"+result.length);
        //打印字符集
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder = stringBuilder.append("E:{");
        for (int i = 0; i < way.length; i++) {
            if (i==way.length-1){
                stringBuilder.append(way[i]);
                break;
            }
            stringBuilder = stringBuilder.append(way[i]+",");
        }
        stringBuilder.append("}");
        fileWrite.write(stringBuilder.toString());
        //打印状态数 和状态集合
        Integer[] stateSet = getWay(result, 0);
        fileWrite.write("States set:"+("1"+"..."+(stateSet.length)));
        fileWrite.write("States number:"+stateSet.length);
        for (int j = 0; j < result.length; j++) {
            //show里面showState对象showState{start=xBuf{x1=2, x2=2}, way='b', end=xBuf{x1=1, x2=3}}
            // 0 -> 1 [label= 1 ];
            String sb = result[j][0] + " -> " +  result[j][2]  + " [label= " +  result[j][1] + " ];";
            //打印输出
            try {
                fileWrite.write(sb);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < finalMarked.size(); i++) {
            sb.append(finalMarked.get(i)).append(" ");
        }
        fileWrite.write( "Marked States:"+sb.toString() );
        fileWrite.writeClose();
    }
    public static Integer[] getWay(int[][] dfa,int chooseLine) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < dfa.length; i++) {
            if (!list.contains(dfa[i][chooseLine]))
                list.add(dfa[i][chooseLine]);
        }
        Integer[] array = list.toArray(new Integer[list.size()]);//能正确        return ;
        Arrays.sort(array);
        return array;
    }
}
